package com.yjx.controller;

import com.github.pagehelper.PageInfo;
import com.yjx.mapper.ParkingMapper;
import com.yjx.service.OwnerService;
import com.yjx.service.ParkingService;
import com.yjx.util.JsonObject;
import com.yjx.util.R;
import com.yjx.vo.Carcharge;
import com.yjx.vo.Owner;
import com.yjx.vo.Parking;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * zjx
 */
@RestController
@RequestMapping("/parking")
public class ParkingController {
    @Resource
    private ParkingService parkingService;
    @Resource
    private ParkingMapper parkingMapper;
    @RequestMapping("/queryParkAll")
    public JsonObject queryParkAll(String numbers,String ownerName,Integer status,
                                   @RequestParam(defaultValue ="1") Integer page,
                                   @RequestParam(defaultValue ="15") Integer limit){

        Parking parking = new Parking();
        parking.setNumbers(numbers);
        parking.setOwnerName(ownerName);
        parking.setStatus(status);
        PageInfo<Parking> pageInfo=parkingService.queryParkAll(page,limit,parking);

        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加车位星系
     * @param parking
     * @return
     */
    @RequestMapping("add")
    public R addPark(@RequestBody Parking parking){
        //设置默认值，使用状态为1
        parking.setStatus(1);
        parking.getNumbers();

        boolean flag = parkingService.addParking(parking);
        if(flag){
        return R.ok();
        }else{
            return R.fail("添加失败");
        }
    }
    @RequestMapping("deleteByIds")
    public R deleteByIds(Integer[] ids){
        boolean flag= parkingService.deleteByIds(ids);
        if(flag){return R.ok();
        }else {
            return R.fail("批量删除失败");
        }

    }

    /**
     * 通过Id更新数据
     * @return
     */
    @RequestMapping("update")
    public R updateParkById(@RequestBody Parking parking){
        Integer id = parking.getId();
        String oldParkingNumber=parkingMapper.selectParkById(id);
        System.out.println(oldParkingNumber);
        boolean flag =parkingService.updateParkingById(parking,oldParkingNumber);
        if(flag) {
            return R.ok();
        }else {
            return R.fail("更新失败");
        }
    }
}
